from django.shortcuts import render
from rest_framework.filters import SearchFilter
from .models import NumbeOfVisits
from utils.commoms import get_list
# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin
from rest_framework.viewsets import GenericViewSet
from .models import ComicCategory, Comic, Comment, Likes
from .serializers import ComicCategorySerializer, ComicSerializer, CommentSerializer, CommentLikeSerializer, \
    NumOfComicSerializer
from app01.utils.page import CommonPageNumberPagination
from utils.response import APIResponse
from rest_framework.response import Response


class ComicCategoryView(GenericViewSet, ListModelMixin):
    queryset = ComicCategory.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class = ComicCategorySerializer


class ComicView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Comic.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = ComicSerializer
    pagination_class = CommonPageNumberPagination
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['comic_category']

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            data = get_list(serializer.data, 4)
            return self.get_paginated_response(data)
        serializer = self.get_serializer(queryset, many=True)
        data = get_list(serializer.data, 4)
        return APIResponse(data=data)


class ComicAllView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Comic.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = ComicSerializer
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['comic_category']


class SearchComicView(GenericViewSet, ListModelMixin):
    queryset = Comic.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = ComicSerializer
    filter_backends = [SearchFilter, ]
    search_fields = ['name']


class ComicCommentView(GenericViewSet, ListModelMixin, CreateModelMixin):
    queryset = Comment.objects.all().order_by('-comment_time')
    serializer_class = CommentSerializer
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['comic']


class CommentLikes(GenericViewSet, CreateModelMixin, ListModelMixin, UpdateModelMixin):
    queryset = Likes.objects.all()
    serializer_class = CommentLikeSerializer
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['commentpk']

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = self.get_serializer(queryset, many=True)
        return APIResponse(data=serializer.data)


class NumofComicView(GenericViewSet, ListModelMixin, CreateModelMixin):
    queryset = NumbeOfVisits.objects.all()
    serializer_class = NumOfComicSerializer


class ComicNum(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Comic.objects.all().filter(is_delete=False, is_show=True)
    serializer_class = ComicSerializer
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['comic_category']

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            order_by_data = sorted(serializer.data, key=lambda x: x['NumofView'], reverse=True)
            return self.get_paginated_response(order_by_data)
        serializer = self.get_serializer(queryset, many=True)
        order_by_data = sorted(serializer.data, key=lambda x: x['NumofView'], reverse=True)
        return Response(order_by_data)
